﻿#include "camera_dalsa.h"


//相机的回调函数
void CameraDalsa::callback(SapXferCallbackInfo *pInfo)
{
    CameraDalsa *pDlg = (CameraDalsa *)pInfo->GetContext();



    bool success=pDlg->m_pBuffers->GetAddress(&pDlg->pData);

    if (!success)
    {
        return;
    }
   
    unsigned char*ucpData;
    ucpData = (unsigned char*)(pDlg->pData);

	
	
    Mat image = cv::Mat(pDlg->m_iImageHeight, pDlg->m_iImageWidth, CV_8U,(void*)ucpData);

    pDlg->m_CallBack(image, pDlg->m_Context);


    pDlg->m_pBuffers->ReleaseAddress(pDlg->pData);

}

void CameraDalsa::registerCallBack(CameraCallback pFunction, void *UserContext)
{
    m_CallBack = pFunction;
    m_Context = UserContext;
    return;
}

bool CameraDalsa::initCamera(const char* name,CameraCallback pfunction, void* pcontext)
{
    registerCallBack(pfunction,pcontext);

    int serverCount = SapManager::GetServerCount();
    if (serverCount == 0)
    {
        return false;
    }
    // 遍历所有型号
    for (int serverIndex = 0; serverIndex < serverCount; serverIndex++)
    {
        int number = SapManager::GetResourceCount(serverIndex, SapManager::ResourceAcqDevice);
        if (number == 0)
        {
            continue;
        }
        char serverName[CORSERVER_MAX_STRLEN];
        SapManager::GetServerName(serverIndex, serverName, sizeof(serverName));
        //遍历型号下面所有相机,寻找序列号正确的相机
        for (int i = 0; i < number; i++)
        {
            char name_now[SapManager::MaxLabelSize];
            if (SapManager::GetResourceName(serverName, SapManager::ResourceAcqDevice, i, name_now, SapManager::MaxLabelSize))
            if (strcmp(name_now, name) == 0)
            {
                m_pAcqDevice = new SapAcqDevice(SapLocation(serverName), false);// 由参数文件加载




                // 指定图像缓冲类
                m_pBuffers = new SapBuffer(2, m_pAcqDevice);
                // 图像传输类
                m_pTransfer = new SapAcqDeviceToBuf(m_pAcqDevice, m_pBuffers, callback, this);

                if (CreateObjects() == false)
                {
                    DestroyObjects();
                    return false;		// 创建对象失败，返回
                }

                m_iImageWidth = m_pBuffers->GetWidth();
                m_iImageHeight = m_pBuffers->GetHeight();




                return true;
            }
        }
    }
    return false;
}

bool CameraDalsa::unInitCamera( )	// 反初始化相机
{
    StopCapture();
    DestroyObjects();
    return true;
}

bool CameraDalsa::StartCapture( )
{
    if (m_pTransfer != nullptr)
    {
        if (m_pTransfer->Grab())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    return true;
}

bool CameraDalsa::StopCapture( )
{
    if (m_pTransfer != nullptr)
    {
        if (false == m_pTransfer->Freeze())
        {
            m_pTransfer->Abort();
        }
    }
    return true;
}

bool CameraDalsa::softTrigger()
{
    if (m_pAcqDevice != nullptr)
    {
        return  m_pAcqDevice->SetFeatureValue("TriggerSoftware", SapAcquisition::SoftwareTriggerExtFrame );
    }
    else
    {
        return false;
    }
}
bool CameraDalsa::SetExp(double exp)
{
    bool status;
    status = m_pAcqDevice->SetFeatureValue("ExposureTime", exp);
    return status;
}

bool CameraDalsa::SetGain(double gain)
{
    bool status;
    status = m_pAcqDevice->SetFeatureValue("Gain", gain);

    return status;//设置增益失败
}

bool CameraDalsa::CreateObjects()
{
    if (!m_pAcqDevice->Create())
    {
        return false;
    }
    if (!m_pBuffers->Create())
    {
        return false;
    }
    if (!m_pTransfer->Create())
    {
        return false;
    }

    return true;
}

bool CameraDalsa::DestroyObjects()
{
//    m_pAcqDevice->SetFeatureValue("TriggerModeSelector", "FrameStart");  // 选择触发阶段
//    m_pAcqDevice->SetFeatureValue("TriggerMode", "On");                   // 启用触发模式


    if (m_pTransfer && *m_pTransfer) m_pTransfer->Destroy();
    if (m_pBuffers && *m_pBuffers) m_pBuffers->Destroy();
    if (m_pAcqDevice && *m_pAcqDevice) m_pAcqDevice->Destroy();
    // Delete all objects

    if (m_pTransfer)		delete m_pTransfer;
    if (m_pBuffers)	delete m_pBuffers;
    if (m_pAcqDevice)		delete m_pAcqDevice;

    m_pTransfer = nullptr;
    m_pBuffers = nullptr;
    m_pAcqDevice = nullptr;
    return true;
}
